const parser = require("@babel/parser");
const template = require("@babel/template").default;
const traverse = require("@babel/traverse").default;
const type = require("@babel/types");
const generator = require("@babel/generator").default;
const path = require('path');
const fs = require('fs');

const { decryptStr, decryptStrFnName } = require('./module'); //此处导包,把函数和函数名导出来
//path.resolve(__dirname, './source.js') 添加绝对路径
const visitor = {
    CallExpression(path) {
        var curNode = path.node //获取到当前方法的节点
        if (curNode.callee.name === decryptStrFnName && curNode.arguments.length === 2) { //判断方法名是不是索要执行的函数名 和传入的值是不附和要求
            var strC = decryptStr(curNode.arguments[0].value, curNode.arguments[1].value); //执行函数
            path.replaceWith(type.stringLiteral(strC)) //path.replaceWith() 替换 type.stringLiteral 字面量  strC执行完的值
        }
    }
}
fs.readFile(path.resolve(__dirname, './source.js'), { "encoding": 'utf-8' }, function (err, data) {
    const ast = parser.parse(data);
    traverse(ast, visitor);
    // 将ast 转为 js
    let { code } = generator(ast);
    // code = code.replace(/!!\[\]/g, 'true').replace(/!\[\]/g, 'false');
    console.log(code,en);
});
